package;

import nme.display.MovieClip;
import nme.display.Bitmap;
import nme.display.Shape;

/**
 * ...
 * @author jonathanmoriarty
 */
class HealthDisplay extends MovieClip, implements Display 
{
	private var _maxValue:Int;
	private var _minValue:Int;
	private var _currValue:Int;

	private var _originalWidth:Float;
	private var _underBar:Bitmap;
	private var _overBar:Bitmap;
	private var _mask:Shape;
	
	public function new( l_x:Int, l_y:Int, l_minValue:Int, l_maxValue:Int, l_startValue:Int ) 
	{
		super();
		this.x = l_x;
		this.y = l_y;
		
		_minValue = l_minValue;
		_maxValue = l_maxValue;
		_currValue = l_startValue;
		
		_underBar = Tools.loadBitmap("bar_empty");
		_underBar.cacheAsBitmap = true;
		addChild( _underBar );
		_originalWidth = _underBar.width;
		
		_overBar = Tools.loadBitmap("bar_full");
		_overBar.cacheAsBitmap = true;
		addChild( _overBar );
		
		_mask = new Shape();
		_mask.graphics.lineStyle();
		_mask.graphics.beginFill( 0x00FF00, 1 );
		_mask.graphics.drawRect( 0, 0, _overBar.width, _overBar.height );
		_mask.graphics.endFill();
		addChild( _mask );
		//_mask.scaleX = -1;
		_overBar.mask = _mask;
	}
	
	public function update( l_deltaTime:Int, l_currValue:Int ):Void
	{
		_currValue = l_currValue;
		if ( _currValue < _minValue )
		{
			_currValue = _minValue;
		}
		else if ( _currValue > _maxValue )
		{
			_currValue = _maxValue;
		}
		
		var l_diff:Float = _mask.width - (_originalWidth * ( _currValue / _maxValue ));
		_mask.width = _originalWidth * ( _currValue / _maxValue );
		_mask.x += l_diff;
	}
}
